package material;

import java.io.File;
import java.io.RandomAccessFile;

public class Copy {
	public static void main(String[] args) {
		File srf = new File("D:/films/abc.wmv");
		File dsf = new File("E:/abc.wmv");
		long size = 20 * 1024 * 1024;
		
		int n = MyCopy.threadNum(srf, size);
		myStart(srf, dsf, n, size);
	}

	private static void myStart(File srf, File dsf, int n, long size) {
            for (int i = 0; i < n; i++) {
				new MyCopy(srf, dsf, i*size, size).start();
			}		
	}

}

class MyCopy extends Thread {
	File srf = null;
	File dsf = null;
	long start;
	long size;

	public MyCopy(File srf, File dsf, long start, long size) {
		super();
		this.srf = srf;
		this.dsf = dsf;
		this.start = start;
		this.size = size;
	}

	public static int threadNum(File srf, long size) {

		return (int) (srf.length() % size == 0 ? (srf.length() / size) : (srf
				.length() / size + 1));
	}

	@Override
	public void run() {
		try {
			RandomAccessFile sraf = new RandomAccessFile(srf, "rw");
			RandomAccessFile draf = new RandomAccessFile(dsf, "rw");
			sraf.seek(start);
			draf.seek(start);
			byte[] bb=new byte[1024*1024];
			int b=0;
			int c=0;
			while((b=sraf.read(bb))!=-1){
				draf.write(bb, 0, b);
				c+=b;
				if(c==size)
					break;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	

}